clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------	
	* For Heiko:
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"

*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
log using "$log/flowsacrossmetro", text replace
********************************************************************************
use "$data_proc\BeH_2pct_cleaned.dta", clear 

rename q quarter

gen eventdate=yq(year,quarter)
format eventdate %tq
drop year quarter

qui: ren district krs
qui: destring krs, force replace

merge m:1 krs using "$data_add/amr_krs_72.dta"
drop if _merge!=3
drop _merge krs

tab amr

xtset prs_id eventdate
gen lag2_amr = l2.amr
gen lag1_amr = l1.amr

by eventdate, sort: egen aux = count(prs_id)
by eventdate, sort: egen aux1 = count(lag1_amr)
by eventdate, sort: egen aux2 = count(lag2_amr)
gen share1= aux1/aux
gen share2= aux2/aux
tabstat share1 share2, by(eventdate)
drop aux* share*

by amr, sort: egen aux = count(prs_id)
by amr, sort: egen aux1 = count(lag1_amr)
by amr, sort: egen aux2 = count(lag2_amr)
gen share1= aux1/aux
gen share2= aux2/aux
tabstat share1 share2, by(amr)

sum
by amr lag1_amr eventdate, sort: egen stock_across1 = count(prs_id)
by amr lag2_amr eventdate, sort: egen stock_across2 = count(prs_id)
sum

preserve
keep amr lag1_amr eventdate stock_across1
duplicates drop
compress
saveold "$data_proc/stock_across1.dta", replace version(14)
restore

keep amr lag2_amr eventdate stock_across2
duplicates drop
compress
saveold "$data_proc/stock_across2.dta", replace version(14)
********************************************************************************
